# _*_coding=utf-8 _*_
import os
import time
import requests
from lxml import etree
from fake_useragent import UserAgent
import pandas as pd

commons_total = []
stars_total = []

"""
名称：mission_05_爬取数据
版本：v1.0
功能：
这个用来是把豆瓣上大护法的数据拿下来的。
时间：2020-12-18
作者：长毛怪^^
"""


def scrap_get():

    ua = UserAgent(verify_ssl=False)
    header = {'user-agent': ua.random}
    url = 'https://movie.douban.com/subject/26811587/comments'
    urls = tuple(f'{url}?start={page * 20}&&limit=20&status=P&sort=new_score' for page in range(10))

    for url_page in urls:
        r = requests.get(url_page, headers=header)
        r.encoding = 'utf-8'
        selector = etree.HTML(r.text)
        commons = selector.xpath('//span[@class="short"]/text()')
        stars = selector.xpath('//span[@class="comment-info"]/span[2]/@title')
        for i in commons:
            commons_total.append(i)
        for i in stars:
            stars_total.append(i)
        time.sleep(1)

    path = os.getcwd() + '/static_resources/'
    if not os.path.exists(path):  # 如果路径不存在就创建路径
        os.makedirs(path)

    data = pd.DataFrame(index=[i for i in range(200)])
    data['commons'] = ''
    data['stars'] = ''

    for i in range(200):
        data['commons'].loc[i] = commons_total[i]
        if stars_total[i] == '力荐':
            data['stars'].loc[i] = 5
        elif stars_total[i] == '推荐':
            data['stars'].loc[i] = 4
        elif stars_total[i] == '还行':
            data['stars'].loc[i] = 3
        elif stars_total[i] == '较差':
            data['stars'].loc[i] = 2
        elif stars_total[i] == '很差':
            data['stars'].loc[i] = 1
        else:
            data['stars'].loc[i] = 0

    data.to_excel(path+'大护法短评.xlsx')


if __name__ == '__main__':
    scrap_get()
